package com.md.youyouhui.ui.user;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityAddCardBinding;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

/**
 * Created by AdministratorZJ
 * on 2019/4/11.
 */
public class AddCardActivity extends BaseActivity {
    private ActivityAddCardBinding cardBinding;
    private MainViewModel viewModel;
    private int type = 1;

    @Override
    public void setContentLayout() {
        cardBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_card);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(cardBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        cardBinding.addCardShi.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                cardBinding.addCardShi.setClickable(false);
                cardBinding.addCardOil.setClickable(true);
                cardBinding.addCardOil.setChecked(false);
                Toast.makeText(ct, "中石化", Toast.LENGTH_SHORT).show();
                type = 1;
            }
        });
        cardBinding.addCardOil.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                cardBinding.addCardOil.setClickable(false);
                cardBinding.addCardShi.setClickable(true);
                cardBinding.addCardShi.setChecked(false);
                Toast.makeText(ct, "中石油", Toast.LENGTH_SHORT).show();
                type = 2;
            }
        });
        //放在监听下面，先监听才能设置成功
        cardBinding.addCardShi.setChecked(true);
        cardBinding.addCardRead.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                cardBinding.addCardBtn.setEnabled(true);
            } else {
                cardBinding.addCardBtn.setEnabled(false);
            }
        });
        cardBinding.addCardBtn.setOnClickListener(v -> bindCard());
    }

    private void bindCard() {
        //1中石化，2中石油
        String num = cardBinding.addCardNum.getText().toString();
        if (TextUtils.isEmpty(num)) {
            Toast.makeText(ct, "请输入油卡卡号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (type == 1) {
            if (!num.startsWith("100011") || num.length() != 19) {
                Toast.makeText(ct, "卡号输入有误", Toast.LENGTH_SHORT).show();
                return;
            }
        } else {
            if (!num.startsWith("90") || num.length() != 16) {
                Toast.makeText(ct, "卡号输入有误", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        String nume = cardBinding.addCardName.getText().toString();
        if (TextUtils.isEmpty(nume)) {
            Toast.makeText(ct, "请输入持卡人姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = cardBinding.addCardPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(ct, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        viewModel.bindCard(num, phone, nume, type).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case LOADING:
                    cardBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    cardBinding.progress.setVisibility(View.GONE);
                    Toast.makeText(ct, "绑定成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case ERROR:
                    cardBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }
}
